package com.uzai.console.service.zombiefan.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uzai.common.enums.DeviceTypeEnum;
import com.uzai.common.enums.KefuSendMsgEnum;
import com.uzai.common.enums.MessageFuncEnum;
import com.uzai.common.enums.MessageTypeEnum;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.msg.MsgDto;
import com.uzai.common.msg.NoticeMsgDto;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.ResponseMsg;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.constant.Const;
import com.uzai.console.dto.device.DeviceQuery;
import com.uzai.console.dto.zombiefan.zombiefantask.ZombieFanTaskDto;
import com.uzai.console.dto.zombiefan.zombiefantask.ZombieFanTaskIdDto;
import com.uzai.console.dto.zombiefan.zombiefantask.ZombieFanTaskIdListDto;
import com.uzai.console.dto.zombiefan.zombiefantask.ZombieFanTaskQuery;
import com.uzai.console.entity.Device;
import com.uzai.console.entity.ZombieFanTask;
import com.uzai.console.enums.ZombieFanTaskStatusEnum;
import com.uzai.console.mapper.ZombieFanMapper;
import com.uzai.console.mapper.ZombieFanTaskMapper;
import com.uzai.console.mapper.ZombieFanTaskSendInfoMapper;
import com.uzai.console.service.device.DeviceService;
import com.uzai.console.service.rocketmq.NoticeMsgPushProducerService;
import com.uzai.console.service.zombiefan.ZombieFanTaskService;
import com.uzai.console.service.zombiefan.ZombieFanTaskSyncService;
import com.uzai.console.vo.device.DeviceInfoVo;
import com.uzai.console.vo.zombiefan.zombiefantask.ZombieFanTaskVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 清理僵尸粉任务
 *
 * @author liuqi
 * @date 2021年08月13日 11:34
 */
@Service
public class ZombieFanTaskServiceImpl implements ZombieFanTaskService {

    Logger logger = LoggerFactory.getLogger(ZombieFanTaskServiceImpl.class);

    @Autowired
    private ZombieFanTaskMapper zombieFanTaskMapper;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private ZombieFanTaskSyncService zombieFanTaskSyncService;
    @Autowired
    private ZombieFanTaskSendInfoMapper zombieFanTaskSendInfoMapper;
    @Autowired
    private ZombieFanMapper zombieFanMapper;
    @Autowired
    private NoticeMsgPushProducerService noticeMsgPushProducerService;

    /**
     * 查询清理僵尸粉任务列表
     *
     * @param zombieFanTaskQuery
     * @return null
     * @author liuqi
     * @date 2021/8/13 11:38
     */
    public Page<ZombieFanTaskVo> findZombieFanTaskList(ZombieFanTaskQuery zombieFanTaskQuery) {

        if (StringUtils.isBlank(zombieFanTaskQuery.getDeviceType())) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "机器人类型不能为空");
        }

        Integer now = DateUtil.getNowTime();

        //新建返回到页面的LIST对象
        List<ZombieFanTaskVo> zombieFanTaskInfoVoList = new ArrayList<>();

        //执行分页查询对象
        Page<ZombieFanTask> page = new Page<>();
        zombieFanTaskQuery.convert(page);
        List<ZombieFanTask> zombieFanTaskList = zombieFanTaskMapper.findByList(page, zombieFanTaskQuery);
        if (zombieFanTaskList != null) {
            for (ZombieFanTask zombieFanTask : zombieFanTaskList) {
                ZombieFanTaskVo zombieFanTaskInfoVo = new ZombieFanTaskVo();
                BeanUtils.copyProperties(zombieFanTask, zombieFanTaskInfoVo);

                //查询群发类型名称
                if (StringUtils.isNotBlank(zombieFanTask.getDeviceType())) {
                    DeviceTypeEnum deviceTypeEnum = DeviceTypeEnum.getByKey(zombieFanTask.getDeviceType());
                    if (deviceTypeEnum != null) {
                        zombieFanTaskInfoVo.setDeviceTypeName(deviceTypeEnum.getRemark());
                    }
                }

                //机器人信息
                if (StringUtils.isNotBlank(zombieFanTask.getDeviceType())) {
                    Device device = deviceService.findDeviceInfoByDeviceUniqueId(zombieFanTask.getDeviceUniqueId(), zombieFanTask.getDeviceType(), zombieFanTask.getMerId());
                    if (device != null) {
                        zombieFanTaskInfoVo.setDeviceId(device.getDeviceId());
                        zombieFanTaskInfoVo.setDeviceName(device.getDeviceName());
                        zombieFanTaskInfoVo.setDeviceRemark(device.getDeviceRemark());
                    }
                }

                //状态名称
                if (zombieFanTask.getStatus() != null) {
                    ZombieFanTaskStatusEnum zombieFanTaskStatusEnum = ZombieFanTaskStatusEnum.getById(zombieFanTask.getStatus());
                    if (zombieFanTaskStatusEnum != null) {
                        zombieFanTaskInfoVo.setStatusName(zombieFanTaskStatusEnum.getDesc());
                    }
                }

                //查询备注
                if (StringUtils.isNotBlank(zombieFanTask.getQueryRemark())) {
                    zombieFanTaskInfoVo.setQueryRemarkList(JSONObject.parseArray(zombieFanTask.getQueryRemark(), String.class));
                }

                //查询僵尸粉总数
                zombieFanTaskInfoVo.setZombieFanTotalNum(zombieFanMapper.selectCount(zombieFanTask.getDeviceUniqueId(), zombieFanTask.getMerId()));
                //查询已清理僵尸粉总数
                zombieFanTaskInfoVo.setZombieFanDelNum(zombieFanMapper.selectDelCount(zombieFanTask.getDeviceUniqueId(), zombieFanTask.getMerId()));

                zombieFanTaskInfoVoList.add(zombieFanTaskInfoVo);

            }
        }
        //返回到页面分页对象
        Page<ZombieFanTaskVo> pageVo = new Page<>();
        BeanUtils.copyProperties(page, pageVo);
        pageVo.setRecords(zombieFanTaskInfoVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }

    /**
     * 查询清理僵尸粉任务详情
     *
     * @param zombieFanTaskIdDto
     * @return null
     * @author liuqi
     * @date 2021/8/13 11:38
     */
    public ZombieFanTaskVo findZombieFanTask(ZombieFanTaskIdDto zombieFanTaskIdDto) {
        //返回到页面的VO对象
        ZombieFanTaskVo zombieFanTaskInfoVo = new ZombieFanTaskVo();

        if (zombieFanTaskIdDto.getId() == null) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }

        ZombieFanTask zombieFanTask = zombieFanTaskMapper.selectById(zombieFanTaskIdDto.getId(), zombieFanTaskIdDto.getMerId());

        if (zombieFanTask == null) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该记录不存在");
        }

        //将查询到的数据复制到页面VO中
        BeanUtils.copyProperties(zombieFanTask, zombieFanTaskInfoVo);

        //查询群发类型名称
        if (StringUtils.isNotBlank(zombieFanTask.getDeviceType())) {
            DeviceTypeEnum deviceTypeEnum = DeviceTypeEnum.getByKey(zombieFanTask.getDeviceType());
            if (deviceTypeEnum != null) {
                zombieFanTaskInfoVo.setDeviceTypeName(deviceTypeEnum.getRemark());
            }
        }

        //机器人信息
        if (StringUtils.isNotBlank(zombieFanTask.getDeviceType())) {
            Device device = deviceService.findDeviceInfoByDeviceUniqueId(zombieFanTask.getDeviceUniqueId(), zombieFanTask.getDeviceType(), zombieFanTask.getMerId());
            zombieFanTaskInfoVo.setDeviceId(device.getDeviceId());
            zombieFanTaskInfoVo.setDeviceName(device.getDeviceName());
            zombieFanTaskInfoVo.setDeviceRemark(device.getDeviceRemark());
        }

        //状态名称
        if (zombieFanTask.getStatus() != null) {
            ZombieFanTaskStatusEnum zombieFanTaskStatusEnum = ZombieFanTaskStatusEnum.getById(zombieFanTask.getStatus());
            if (zombieFanTaskStatusEnum != null) {
                zombieFanTaskInfoVo.setStatusName(zombieFanTaskStatusEnum.getDesc());
            }
        }

        //查询备注
        if (StringUtils.isNotBlank(zombieFanTask.getQueryRemark())) {
            zombieFanTaskInfoVo.setQueryRemarkList(JSONObject.parseArray(zombieFanTask.getQueryRemark(), String.class));
        }

        return zombieFanTaskInfoVo;
    }

    /**
     * 添加清理僵尸粉任务
     *
     * @param zombieFanTaskDto
     * @return null
     * @author liuqi
     * @date 2021/8/13 11:38
     */
    public void addZombieFanTask(ZombieFanTaskDto zombieFanTaskDto) {

        if (StringUtils.isBlank(zombieFanTaskDto.getDeviceType())) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "机器人类型不能为空");
        }

        if (zombieFanTaskDto.getDeviceUniqueIdList() == null || zombieFanTaskDto.getDeviceUniqueIdList().size() == 0) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择机器人");
        }

        if (StringUtils.isBlank(zombieFanTaskDto.getName())) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入任务名称");
        }

        //循环机器人，依次建立每个机器人的清粉任务
        if (zombieFanTaskDto.getDeviceType().equals(DeviceTypeEnum.DT_PC_WECHAT.getKey())) {

            if (zombieFanTaskDto.getCheckType() == null) {
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择检查方式");
            }

            if (zombieFanTaskDto.getCheckType().intValue() == 1) { //发送消息检查方式
                if (StringUtils.isBlank(zombieFanTaskDto.getCheckMsg())) {
                    throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入检查文本消息");
                }
            }

            if (zombieFanTaskDto.getCheckMin() == null || zombieFanTaskDto.getCheckMax() == null) {
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入检查时间间隔");
            }

            if (zombieFanTaskDto.getCheckMin() > zombieFanTaskDto.getCheckMax()) {
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "检查时间间隔最小时间不能大于最大时间");
            }

            if (zombieFanTaskDto.getDelMin() == null || zombieFanTaskDto.getDelMax() == null) {
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入清粉时间间隔");
            }

            if (zombieFanTaskDto.getDelMin() > zombieFanTaskDto.getDelMax()) {
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "清粉时间间隔最小时间不能大于最大时间");
            }

            if (zombieFanTaskDto.getDelDayLimit() == null) {
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入每日清理上限");
            }

            zombieFanTaskSyncService.addZombieFanTaskByWechat(zombieFanTaskDto);
        } else if (zombieFanTaskDto.getDeviceType().equals(DeviceTypeEnum.DT_WORK_WECHAT.getKey())) {
            if (zombieFanTaskDto.getDelMin() == null || zombieFanTaskDto.getDelMax() == null) {
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入清粉时间间隔");
            }
            if (zombieFanTaskDto.getDelMin() > zombieFanTaskDto.getDelMax()) {
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "清粉时间间隔最小时间不能大于最大时间");
            }
            if (zombieFanTaskDto.getDelDayLimit() == null) {
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入每日清理上限");
            }
            zombieFanTaskSyncService.addZombieFanTaskByWxwork(zombieFanTaskDto);
        } else {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "群发任务目前只支持个人微信和企业微信");
        }
    }

    /**
     * 开始清理僵尸粉任务
     *
     * @param zombieFanTaskIdDto
     * @return null
     * @author liuqi
     * @date 2021/8/13 11:38
     */
    @Transactional
    public void startZombieFanTask(ZombieFanTaskIdDto zombieFanTaskIdDto) {
        if (zombieFanTaskIdDto.getId() == null) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }

        ZombieFanTask zombieFanTask = zombieFanTaskMapper.selectById(zombieFanTaskIdDto.getId(), zombieFanTaskIdDto.getMerId());

        if (zombieFanTask == null) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该记录不存在");
        }

        if (zombieFanTask.getStatus().intValue() != ZombieFanTaskStatusEnum.NO_START.getId().intValue()
                && zombieFanTask.getStatus().intValue() != ZombieFanTaskStatusEnum.CHECK_PAUSE.getId().intValue()
                && zombieFanTask.getStatus().intValue() != ZombieFanTaskStatusEnum.DEL_PAUSE.getId().intValue()
        ) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "只有未开始、检查暂停、清粉暂停等状态才能开启任务");
        }

        Integer now = DateUtil.getNowTime();

        //个人微信机器人
        if (zombieFanTask.getDeviceType().equals(DeviceTypeEnum.DT_PC_WECHAT.getKey())) {
            if (zombieFanTask.getStatus().intValue() == ZombieFanTaskStatusEnum.DEL_PAUSE.getId().intValue()) {
                zombieFanTask.setStatus(ZombieFanTaskStatusEnum.DELETING.getId());
            } else {
                zombieFanTask.setStatus(ZombieFanTaskStatusEnum.CHECKING.getId());
            }
            zombieFanTask.setUpdateTime(now);
            zombieFanTaskMapper.updateById(zombieFanTask);
        } else if (zombieFanTask.getDeviceType().equals(DeviceTypeEnum.DT_WORK_WECHAT.getKey())) { //企业微信机器人
            if (zombieFanTask.getStatus().intValue() == ZombieFanTaskStatusEnum.DEL_PAUSE.getId().intValue()) {
                zombieFanTask.setStatus(ZombieFanTaskStatusEnum.DELETING.getId());
                zombieFanTask.setUpdateTime(now);
                zombieFanTaskMapper.updateById(zombieFanTask);
            } else {
                zombieFanTask.setStatus(ZombieFanTaskStatusEnum.CHECKING.getId());
                zombieFanTask.setUpdateTime(now);
                int j = zombieFanTaskMapper.updateById(zombieFanTask);
                if (j == 1) {
                    //发送企业微信清粉指令
                    MsgDto<NoticeMsgDto> msgDto = new MsgDto<>();
                    NoticeMsgDto noticeMsgDto = new NoticeMsgDto();
                    msgDto.setContent(noticeMsgDto);
                    msgDto.setId(IdWorker.getId());
                    msgDto.setDeviceType(DeviceTypeEnum.DT_WORK_WECHAT_VALUE);
                    msgDto.setDeviceUniqueId(zombieFanTask.getDeviceUniqueId());
                    msgDto.setMerId(zombieFanTask.getMerId());
                    msgDto.setMsgType(MessageFuncEnum.SEND_CHAT_MESSAGE.getValue());
                    msgDto.setCsMsg(true); //客服消息不走悠聊系统
                    noticeMsgDto.setContent(Const.ZonbieFan.content_wxwork); //指定的清理僵尸粉内容
                    noticeMsgDto.setBizUserId(Const.ZonbieFan.content_wxwork);
                    noticeMsgDto.setContentType(MessageTypeEnum.MT_NAME_CARD.getValue());
                    noticeMsgDto.setGroupId(null);
                    noticeMsgDto.setNickName(null);
                    noticeMsgDto.setMsgSource(1);
                    noticeMsgDto.setDelay(0);
                    noticeMsgDto.setMsgBizType(KefuSendMsgEnum.UNSOLICITEDMSG.getKey());
                    noticeMsgPushProducerService.sendMessage(msgDto);
                    logger.info("--发送企业微信获取僵尸粉接口指令--dto={}", JSONObject.toJSONString(msgDto));
                } else {
                    throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.NET_ERROR);
                }
            }
        } else {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "群发任务目前只支持个人微信和企业微信");
        }

    }

    /**
     * 批量开始清理僵尸粉任务(只能开启未开始和已暂停、清粉暂停任务)
     *
     * @param zombieFanTaskIdListDto
     * @return null
     * @author liuqi
     * @date 2021/8/13 11:38
     */
    public void batchStartGroupSendJob(ZombieFanTaskIdListDto zombieFanTaskIdListDto) {
        List<Long> idList = zombieFanTaskIdListDto.getIdList();
        if (idList == null || idList.size() == 0) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择批量开启的记录");
        }

        for (Long id : idList) {
            try {
                ZombieFanTaskIdDto zombieFanTaskIdDto = new ZombieFanTaskIdDto();
                zombieFanTaskIdDto.setId(id);
                zombieFanTaskIdDto.setMerId(zombieFanTaskIdListDto.getMerId());
                startZombieFanTask(zombieFanTaskIdDto);
            } catch (Exception e) {
                logger.error("批量开始清理僵尸粉任务出错！error={}", e.getMessage());
            }
        }
    }

    /**
     * 暂停清理僵尸粉任务
     *
     * @param zombieFanTaskIdDto
     * @return null
     * @author liuqi
     * @date 2021/8/13 11:38
     */
    public void pauseZombieFanTask(ZombieFanTaskIdDto zombieFanTaskIdDto) {
        if (zombieFanTaskIdDto.getId() == null) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }

        ZombieFanTask zombieFanTask = zombieFanTaskMapper.selectById(zombieFanTaskIdDto.getId(), zombieFanTaskIdDto.getMerId());

        if (zombieFanTask == null) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该记录不存在");
        }

        if (zombieFanTask.getStatus().intValue() != ZombieFanTaskStatusEnum.CHECKING.getId().intValue()
                && zombieFanTask.getStatus().intValue() != ZombieFanTaskStatusEnum.DELETING.getId().intValue()
        ) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "只有检查中、清粉中等状态才能开启任务");
        }

        Integer now = DateUtil.getNowTime();
        if (zombieFanTask.getStatus().intValue() == ZombieFanTaskStatusEnum.CHECKING.getId().intValue()) {
            zombieFanTask.setStatus(ZombieFanTaskStatusEnum.CHECK_PAUSE.getId());
        } else {
            zombieFanTask.setStatus(ZombieFanTaskStatusEnum.DEL_PAUSE.getId());
        }
        zombieFanTask.setUpdateTime(now);
        zombieFanTaskMapper.updateById(zombieFanTask);
    }

    /**
     * 批量暂停清理僵尸粉任务（只有检查中、清粉中等状态才能开启任务）
     *
     * @param zombieFanTaskIdListDto
     * @return null
     * @author liuqi
     * @date 2021/8/13 11:38
     */
    public void batchPauseGroupSendJob(ZombieFanTaskIdListDto zombieFanTaskIdListDto) {
        List<Long> idList = zombieFanTaskIdListDto.getIdList();
        if (idList == null || idList.size() == 0) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择批量暂停的记录");
        }

        for (Long id : idList) {
            try {
                ZombieFanTaskIdDto zombieFanTaskIdDto = new ZombieFanTaskIdDto();
                zombieFanTaskIdDto.setId(id);
                zombieFanTaskIdDto.setMerId(zombieFanTaskIdListDto.getMerId());
                pauseZombieFanTask(zombieFanTaskIdDto);
            } catch (Exception e) {
                logger.error("批量开始清理僵尸粉任务出错！error={}", e.getMessage());
            }
        }
    }

    /**
     * 删除清理僵尸粉任务
     *
     * @param zombieFanTaskIdDto
     * @return null
     * @author liuqi
     * @date 2021/8/13 11:38
     */
    @Transactional
    public void deleteZombieFanTask(ZombieFanTaskIdDto zombieFanTaskIdDto) {

        if (zombieFanTaskIdDto.getId() == null) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }

        ZombieFanTask zombieFanTask = zombieFanTaskMapper.selectById(zombieFanTaskIdDto.getId(), zombieFanTaskIdDto.getMerId());

        if (zombieFanTask == null) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该记录不存在");
        }

//        if(zombieFanTask.getStatus().intValue() == ZombieFanTaskStatusEnum.INIT.getId().intValue()){
//            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "初始化中的任务不能删除");
//        }

        //删除任务
        zombieFanTaskMapper.deleteById(zombieFanTask.getId(), zombieFanTaskIdDto.getMerId());

        //删除清理僵尸粉发送明细
        zombieFanTaskSendInfoMapper.deleteByTaskId(zombieFanTask.getId(), zombieFanTaskIdDto.getMerId());
    }

    /**
     * 批量删除清理僵尸粉任务（只支持完成状态）
     *
     * @param zombieFanTaskIdListDto
     * @return null
     * @author liuqi
     * @date 2021/8/13 11:38
     */
    public void batchDeleteZombieFanTask(ZombieFanTaskIdListDto zombieFanTaskIdListDto) {
        List<Long> idList = zombieFanTaskIdListDto.getIdList();
        if (idList == null || idList.size() == 0) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择批量删除的记录");
        }
        for (Long id : idList) {
            try {
                ZombieFanTask zombieFanTask = zombieFanTaskMapper.selectById(id, zombieFanTaskIdListDto.getMerId());

                if (zombieFanTask == null) {
                    throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该记录不存在");
                }
                //批量删除只支持已经完成状态
                if (zombieFanTask.getStatus().intValue() != ZombieFanTaskStatusEnum.COMPLETED.getId().intValue()) {
                    throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "批量只能删除完成状态任务");
                }

                //删除任务
                zombieFanTaskMapper.deleteById(zombieFanTask.getId(), zombieFanTask.getMerId());

                //删除清理僵尸粉发送明细
                zombieFanTaskSendInfoMapper.deleteByTaskId(zombieFanTask.getId(), zombieFanTask.getMerId());
            } catch (Exception e) {
                logger.error("批量删除僵尸粉任务失败！error={}", e.getMessage());
            }
        }
    }

    /***
     * 查询群内机器人列表-不分页
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param deviceQuery
     * @return null
     */
    public List<DeviceInfoVo> findDeviceList(DeviceQuery deviceQuery) {
        //封装过滤掉已经配置了清理僵尸任务的机器人
        List<DeviceInfoVo> deviceInfoVoList_new = new ArrayList<>();
        //查询所有的机器人
        List<DeviceInfoVo> deviceInfoVoList = deviceService.findDeviceList(deviceQuery);

        //过滤已经配置了清理僵尸粉任务的机器人
        if (deviceInfoVoList != null && deviceInfoVoList.size() > 0) {
            for (DeviceInfoVo deviceInfoVo : deviceInfoVoList) {
                ZombieFanTask zombieFanTask = zombieFanTaskMapper.selectByDeviceUniqueId(deviceInfoVo.getId(), deviceQuery.getMerId());
                if (zombieFanTask == null) {//查询为空，则未配置清理僵尸任务
                    deviceInfoVoList_new.add(deviceInfoVo);
                }
            }
        }
        return deviceInfoVoList_new;
    }

    /**
     * 修改清理僵尸粉任务
     *
     * @param zombieFanTaskDto
     * @return null
     * @author liuqi
     * @date 2021/8/13 11:38
     */
    public void updateZombieFanTask(ZombieFanTaskDto zombieFanTaskDto) {
        if (zombieFanTaskDto.getId() == null) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }

        ZombieFanTask zombieFanTask = zombieFanTaskMapper.selectById(zombieFanTaskDto.getId(), zombieFanTaskDto.getMerId());

        if (zombieFanTask == null) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该记录不存在");
        }

        if (zombieFanTask.getStatus().intValue() != ZombieFanTaskStatusEnum.NO_START.getId().intValue()
                && zombieFanTask.getStatus().intValue() != ZombieFanTaskStatusEnum.CHECK_PAUSE.getId().intValue()
                && zombieFanTask.getStatus().intValue() != ZombieFanTaskStatusEnum.DEL_PAUSE.getId().intValue()
        ) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "只有未开始、检查暂停、清粉暂停等状态才能修改任务");
        }

        Integer now = DateUtil.getNowTime();
        if (zombieFanTask.getStatus().intValue() == ZombieFanTaskStatusEnum.NO_START.getId().intValue()
          || zombieFanTask.getStatus().intValue() == ZombieFanTaskStatusEnum.CHECK_PAUSE.getId().intValue()) {//未开始，能修改检查设置和删除设置
            //修改检查设置
            zombieFanTask.setCheckType(zombieFanTaskDto.getCheckType());
            zombieFanTask.setCheckMin(zombieFanTaskDto.getCheckMin());
            zombieFanTask.setCheckMax(zombieFanTaskDto.getCheckMax());
            zombieFanTask.setZombieRemark(zombieFanTaskDto.getZombieRemark());
            //修改删除设置
            zombieFanTask.setDelMin(zombieFanTaskDto.getDelMin());
            zombieFanTask.setDelMax(zombieFanTaskDto.getDelMax());
            zombieFanTask.setDelDayLimit(zombieFanTaskDto.getDelDayLimit());
            zombieFanTask.setDelUser(Tools.getInteger(zombieFanTaskDto.getDelUser()));
            zombieFanTask.setDelUserRemark(zombieFanTaskDto.getDelUserRemark());

        } else if(zombieFanTask.getStatus().intValue() == ZombieFanTaskStatusEnum.DEL_PAUSE.getId().intValue()){
            //修改删除设置
            zombieFanTask.setDelMin(zombieFanTaskDto.getDelMin());
            zombieFanTask.setDelMax(zombieFanTaskDto.getDelMax());
            zombieFanTask.setDelDayLimit(zombieFanTaskDto.getDelDayLimit());
            zombieFanTask.setDelUser(Tools.getInteger(zombieFanTaskDto.getDelUser()));
            zombieFanTask.setDelUserRemark(zombieFanTaskDto.getDelUserRemark());
        }

        zombieFanTask.setUpdateTime(now);
        zombieFanTaskMapper.updateById(zombieFanTask);
    }
}
